<link rel="import" href="../polymer/polymer.html">

<!-- tf-url-generator is a plumbing component that provides upward bindable properties
  for each route that TensorBoard uses to retrieve data from the backend. The properties
  are generally functions that take in a run and a tag and produce a url for retrieving
  data for that run and tag (although there are exceptions: for example, outRunsUrl is just
  a string because no additional data is required to retrieve runs).

  tf-url-generator takes a "router" object that provides the functions that back each
  property - that makes it easy to overwrite how TensorBoard retrieves data from the
  backend, e.g. for creating demo instances.
 -->
<dom-module id="tf-url-generator">
  <script src="urlGenerator.js"></script>
  <script>
    var polymerObject = {
      is: "tf-url-generator",
      _computeRuns: function(router) {
        return router.runs();
      },
      properties: {
        router: {
          type: Object,
        },
        outRunsUrl: {
          type: String,
          computed: "_computeRuns(router)",
          readOnly: true,
          notify: true,
        },
      },
    };
    TF.Urls.routes.forEach(function(route) {
      /* for each route (other than runs, handled separately):
       * out`RouteName`: {
       *  type: Function,
       *  readOnly: true,
       *  notify: true,
       *  value: function() {
       *    return TF.Urls.`routeName`Url;
       *  }
       */
      if (route === "runs") {
        return;
      }
      var computeFnName = "_compute" + route;
      polymerObject[computeFnName] = function(router) {
        return router[route];
      };
      var urlName = route + "Url";
      var propertyName = Polymer.CaseMap.dashToCamelCase("out-" + urlName + "Generator");
      polymerObject.properties[propertyName] = {
        type: Function,
        computed: computeFnName + "(router)",
        notify: true,
      }
    });
    Polymer(polymerObject);
  </script>
</dom-module>
